package cn.jetpiece.cloud.config;

import cn.jetpiece.cloud.loadbalancer.JetpieceNacosLoadBalancer;
import com.alibaba.cloud.nacos.NacosDiscoveryProperties;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.loadbalancer.reactive.ReactiveLoadBalancer;
import org.springframework.cloud.loadbalancer.annotation.LoadBalancerClients;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.support.LoadBalancerClientFactory;
import org.springframework.context.annotation.Bean;
import org.springframework.core.env.Environment;

/**
 * 负载均衡策略配置
 *
 * @author XuJZ
 */
@LoadBalancerClients(defaultConfiguration = {JetpieceClusterConfiguration.class})
public class JetpieceClusterConfiguration {

    @Bean
    ReactiveLoadBalancer<ServiceInstance> jetpieceLoadBalancer(Environment environment,
                                                               LoadBalancerClientFactory loadBalancerClientFactory,
                                                               NacosDiscoveryProperties nacosDiscoveryProperties) {
        String name = environment.getProperty(LoadBalancerClientFactory.PROPERTY_NAME);
        return new JetpieceNacosLoadBalancer(
                loadBalancerClientFactory.getLazyProvider(name,
                        ServiceInstanceListSupplier.class),
                name, nacosDiscoveryProperties);
    }
}
